<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:05
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\model\mysql;

use app\common\lib\Status;
use app\common\lib\Time;
use think\Exception;

class Order extends Base
{
    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderList(int $limit = 10, int $page = 0, $where = [])
    {
        return $this->where($where)
            ->where('payStatus', 1)
            ->where('state', 1)
            ->order("id desc")
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ])
            ->toArray();
    }

    /**
     * @param string $order_id
     * @param string $field
     * @return Order|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByOrderId(string $order_id = '', string $field = '*')
    {
        return $this->field($field)
            ->where('order_id', $order_id)
            ->find();
    }

    /**
     * @param array $where
     * @param int $limit
     * @param int $page
     * @param string $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getUserOrder(array $where = [], int $limit = 10, int $page = 1, string $field = '*'): array
    {
        return $this->field($field)
            ->where($where)
            ->where('status', '=', Status::status_normal)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'var_page' => 'page',
                "page" => $page
            ])
            ->toArray();
    }


    public function getSetSexAttr($value)
    {
//        if ($value == 0) {
//            return "男";
//        } else if ($value == 1) {
//            return "女";
//        } else {
//            return "保密";
//        }
        $sex = (new Sex())->getBaseById((int)$value);
        return $sex['title'] ?? "";
    }

    /**
     * 重量
     * @param $value
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSetWeightAttr($value)
    {
        $weight = (new Weight())->getBaseById((int)$value);
        return $weight['title'];
    }

    /**
     * 抢单限制
     * @param $value
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSetXianAttr($value)
    {
        $xian = (new Grabbing())->getBaseById((int)$value);
        return $xian['title'];
    }

    public function getSetGuiAttr($value)
    {
        $gui = (new Gui())->getBaseById((int)$value);
        if (!$gui) {
            return "";
        }
        if ($gui['title'] == "贵重") {
            return "必须当面";
        } else {
            return "无需当面";
        }
    }

    /**
     * 体积描述
     * @param $value
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSetDescAttr($value)
    {
        $desc = (new Volume())->getBaseById((int)$value);
        return $desc['title'];
    }

    /**
     * 验证是否上楼
     * @param $value
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getSetLouAttr($value)
    {
        $lou = (new Lou())->getBaseById((int)$value);
        return $lou['title'];
    }

    /**
     * @param array $where
     * @param string $field
     * @return Order|array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPlaceOrderByCount(array $where = [], $field = '*')
    {
        return $this->field($field)
            ->where($where)
            ->find();
    }

    /**
     * @param $value
     * @return int|mixed
     */
    public function getAuthIdAttr($value)
    {
        $order = (new GrabSheet())->getGrabOrderByOrderId((int)$value, "id");
        return $order['id'] ?? 0;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByStateTime()
    {
        $time = time() - 172800;
        return $this->where('state_time', '<', $time)
            ->where('state', 'in', [7, 5])
            ->select()
            ->toArray();
    }

    /**
     * @param array $ids
     * @return bool
     * @throws Exception
     */
    public function updateIdsByState(array $ids = [])
    {
        $this->startTrans();
        try {
            $update_data = [
                "state" => 4,
                "update_time" => time()
            ];
            $this->where('id', 'in', $ids)
                ->update($update_data);
            (new Money())->where('order_id', 'in', $ids)->update([
                "update_time" => time(),
                "state" => 1
            ]);
            $this->commit();
        } catch (\Exception $e) {
            $this->rollback();
            throw new Exception("内部错误");
        }
        return true;
    }
}